package com.liferay.outil;

import java.io.UnsupportedEncodingException;

import javax.mail.internet.AddressException;
import javax.mail.internet.InternetAddress;

import com.liferay.portal.kernel.mail.MailMessage;
import com.liferay.portal.kernel.messaging.DestinationNames;
import com.liferay.portal.kernel.messaging.MessageBusUtil;
import com.liferay.portal.model.User;

public class OaUtil {
	static String toName="";
	 static String dispUrl="";
	 public static void sendMail(User objUser,String Title, String strBody) throws AddressException
	   {
		 String Email=objUser.getEmailAddress();
	   	 MailMessage M=new MailMessage();
	   	 M.setTo(new InternetAddress(Email));
	   	 M.setBody(strBody);
	   	 M.setSubject(Title);
	   	 M.setFrom(new InternetAddress("noreply@medicalassociation.in"));
	   	 M.setHTMLFormat(true);
	   	 MessageBusUtil.sendMessage(DestinationNames.MAIL, M);
	   }
	 //Thsi method is for medicalassociation member user
	 public static void sendEMailToMember(String strFromName, String strFromAddress, String strSubject, String strBody, User u, String strDispUrl) throws UnsupportedEncodingException, AddressException
	   {
		 toName=u.getFullName();
		 dispUrl=strDispUrl;
		 MailMessage M=new MailMessage();
		 M.setFrom(new InternetAddress(strFromAddress, strFromName));
	   	 M.setTo(new InternetAddress(u.getEmailAddress()));
	   	 M.setBody(makeMailPage(strBody));
	   	 M.setSubject(strSubject);
	   	 M.setHTMLFormat(true);
	   	 MessageBusUtil.sendMessage(DestinationNames.MAIL, M);
	   }
	 //This method is for others who r not member of medicalassociation.in
	 public static void sendEMail(String strFromName, String strFromAddress, String strToName, String strToAddress, String strSubject, String strBody) throws UnsupportedEncodingException, AddressException
	 {
		 toName=strToName;
		 dispUrl="";
		 MailMessage M=new MailMessage();
		 M.setFrom(new InternetAddress(strFromAddress, strFromName));
	 	 M.setTo(new InternetAddress(strToAddress));
	 	 M.setBody(makeMailPage(strBody));
	 	 M.setSubject(strSubject);
	 	 M.setHTMLFormat(true);
	 	 MessageBusUtil.sendMessage(DestinationNames.MAIL, M);
	 }
	 
	 public static String makeMailPage(String body)
	  {
		 String strMailPage="<table  width=\"90%\" align=\"center\">" +
		 						"<tr style=\"font-size:20px;font-weight:bolder;\">" + getHeader()+
		 						"</tr>" +
		 						"<tr><td>"+body+"</td></tr>" +
		 						"<tr><td style=\"font-size:14px;\">"+getFooter()+"</td></tr>" +
		 					"</table>";
		 return strMailPage;
	  }
	private static String getHeader()
	 {
		
	String strHeader="<tr style=\"background-color:#014b68;color:gray;text-shadow: -1px -1px 1px white, 1px 1px 1px black;font: 25px 'Museo700'; min-height:70px;\"> <a  href=\"http://medicalassociation.in\" style=\"padding:5px;color:#fff;\"><img src=\"http://medicalassociation.in/images/icon/logo.png\" alt=\"MedicalAssociation\"></a></tr>";
		if (toName.length()>0)
		  {
			strHeader+="<tr><td>Hi," + toName+ "<a style=\"float:right\" href=\""+dispUrl+"\">View your Doctors Pages</td></tr>";
			//add here toName in html formate
		  }
		
		return strHeader;
	 }
	private static String getFooter()
	{
		String strFooter=" <hr/> Thanks,<br/>Team MedicalAssociation<br/><a href=\"http://medicalassociation.in \">MedicalAssociation </a><br/> admin@medicalassociation.in <br/>This email has been sent to :[[EMAIL_TO]],&nbsp;<a href=\"[[UNSUB_LINK_EN]]\" style=\"color:#000000\">click here to unsubscribe</a> ";
		//strFooter+="<br/><a href=\""+dispUrl+"\">"+ dispUrl;
		
		
		return strFooter;
	}
}
